/*******************************************************************************************
Copyright (C) 2013 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*******************************************************************************************/

/*globals print*/
/*jslint plusplus: true, indent: 4, nomen: true*/

var Utl = {};

Utl.callEval = function callEval(aStr) {
	"use strict";
	try {
		/*jslint evil: true*/
		var a = eval(aStr);
		/*jslint evil: false*/
		return a;
	} catch (e) {
		return "Error: " + e.name + " - " + e.message;
	}
};

// Sum all the arguments
// Usage: sum(1, 2);
//		sum(1, 2, 3);
//		sum("a", "b", "c", "d");
Utl.sum = function sum() {
	"use strict";
	var result, i;
	if (!arguments.length) {
		return result;
	}
	result = arguments[0];
	for (i = 1; i < arguments.length; i++) {
		result += arguments[i];
	}
	return result;
};

Utl.sub = function sub(a, b) {
	"use strict";
	return a - b;
};

Utl.mul = function mul(a, b) {
	"use strict";
	return a * b;
};

Utl.div = function div(a, b) {
	"use strict";
	/*jslint eqeq: true*/
	if (b == 0) {
		return 0;
	}
	/*jslint eqeq: false*/
	return a / b;
};

Utl.C2K = function C2K(celsius) {
	"use strict";
	return celsius + 273.15;
};

Utl.K2C = function K2C(kelvin) {
	"use strict";
	return kelvin - 273.15;
};

Utl.C2F = function C2F(celsius) {
	"use strict";
	return celsius * 1.8 + 32;
};

Utl.F2C = function F2C(fahrenheit) {
	"use strict";
	return (fahrenheit - 32) / 1.8;
};

Utl.K2F = function K2F(kelvin) {
	"use strict";
	return kelvin * 1.8 - 459.67;
};

Utl.F2K = function F2K(fahrenheit) {
	"use strict";
	return (fahrenheit + 459.67) / 1.8;
};

Utl.defineGlobalProperty = function defineGlobalProperty(aGlobalObj, aObj, aPropStr) {
	"use strict";
	if (aGlobalObj[aPropStr] && aGlobalObj[aPropStr] !== aObj[aPropStr]) {
		print("WARNING: redefinition of the global object property '{0}'".format(aPropStr));
	}
	aGlobalObj[aPropStr] = aObj[aPropStr];
};

Utl.pathDistance = function pathDistance(aPath1, aPath2)
{
	var splitRE = /[\/\\]+/,
		path1Arr = aPath1.split(splitRE),
		path2Arr = aPath2.split(splitRE),
		firstName = "",
		secondName = "",
		minLength = 0,
		i = 0,
		fineFactor = 0;

	if (aPath1 === aPath2) {
		return 0;
	}

	while (path1Arr.length && path2Arr.length) {
		if (path1Arr[0] === path2Arr[0]) {
			path1Arr.shift();
			path2Arr.shift();
		} else {
			firstName = path1Arr[0];
			secondName = path2Arr[0];
			if (firstName.length < secondName.length) {
				minLength = firstName.length;
			} else {
				minLength = secondName.length;
			}

			for (i = 0; i < minLength; ++i) {
				if (firstName.charAt(i) !== secondName.charAt(i)) {
					break;
				}
			}

			if (i) {
				fineFactor = 1.0 / i;
			}
			break;
		}
	}

	return path1Arr.length + path2Arr.length + fineFactor;
}

// Global functions definitions for backward compatibility
Utl.defineGlobalProperty(this, Utl, "callEval");
Utl.defineGlobalProperty(this, Utl, "sum");
Utl.defineGlobalProperty(this, Utl, "sub");
Utl.defineGlobalProperty(this, Utl, "mul");
Utl.defineGlobalProperty(this, Utl, "div");
Utl.defineGlobalProperty(this, Utl, "C2K");
Utl.defineGlobalProperty(this, Utl, "K2C");
Utl.defineGlobalProperty(this, Utl, "C2F");
Utl.defineGlobalProperty(this, Utl, "F2C");
Utl.defineGlobalProperty(this, Utl, "K2F");
Utl.defineGlobalProperty(this, Utl, "F2K");
